package com.example.shop_2208.order

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2208.R
import com.example.shop_2208.adapter.OrderAdapter
import com.example.shop_2208.databinding.ActivityOrderBinding
import com.example.shop_2208.db.MyDbUtils
import com.example.shop_2208.mvvm.BaseViewActivity
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity

@Route(path = "/app/OrderActivity")
class OrderActivity : BaseViewActivity<ActivityOrderBinding>() {
    lateinit var orderAdapter: OrderAdapter
    override fun initData() {
    }

    override fun initView() {
        orderAdapter = OrderAdapter()
        binding.rvOrder.adapter = orderAdapter
        binding.rvOrder.layoutManager = LinearLayoutManager(this)

        val data = MyDbUtils.getDb().getOrderDao().queryStatus(0)//查询所有待下单的订单
        orderAdapter.data.clear()
        orderAdapter.data.addAll(data)
        orderAdapter.notifyDataSetChanged()

        var money = 0
        data.forEach {
            money+=it.count*it.goods_default_price
        }
        binding.tvMoney.text = "$money 元"

        binding.btnPay.setOnClickListener {
            ARouter.getInstance().build("/app/PayActivity").navigation()
            finish()//结束当前界面
        }
        //显示用户地址
        val address = SPUtils.getInstance().getString("address")
        binding.tvAddress.text = "用户地址：$address"

        //弹出地址选择框
        binding.tvAddress.setOnClickListener {
            val addressPicker = AddressPicker(this)
            addressPicker.setTitle("选择配送地址")
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)//显示省-市-县
            addressPicker.setOnAddressPickedListener { province, city, county ->
                ToastUtils.showLong("${province.name}-${city.name}-${county.name}")
                binding.tvAddress.text = "用户地址：${province.name}-${city.name}-${county.name}"
                SPUtils.getInstance().put("address","${province.name}-${city.name}-${county.name}")
            }
            addressPicker.show()
            //日期选择框
           /* val datePicker = DatePicker(this)
            datePicker.setTitle("选择出生年月")
            datePicker.wheelLayout.setRange(DateEntity.target(1900,1,1), DateEntity.today())//设置日期范围
            datePicker.wheelLayout.setDefaultValue(DateEntity.today())//设置默认选中时间
            datePicker.setOnDatePickedListener { year, month, day ->
                ToastUtils.showLong("$year / $month /$day")
            }
            datePicker.show()*/
            //选项选择框--经常用于性别、职业
           /* val list = mutableListOf("男","女","未知")
            val optionPicker = OptionPicker(this)
            optionPicker.setTitle("请选择性别")
            optionPicker.setData(list)
            optionPicker.setOnOptionPickedListener { position, item ->
                ToastUtils.showLong("选择的是$item")
            }
            optionPicker.show()*/
        }

    }

    override fun getLayout(): Int  = R.layout.activity_order

}